package com.guldan.jaina.manager;

import com.guldan.core.manager.BaseManager;
import com.guldan.core.search.Pagination;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.registerfail.RegisterFailDTO;
import com.guldan.jaina.dto.registerfail.RegisterFailSearchDTO;
import com.guldan.jaina.dto.registersuccess.RegisterSuccessDTO;
import com.guldan.jaina.utils.RegisterFailDomainConvertUtils;
import com.guldan.jaina.utils.RegisterSuccessDomainConvertUtils;
import com.guldan.jaina.utils.UserUtils;
import com.guldan.mapper.db.entity.*;
import com.guldan.mapper.db.mapper.AreaMapperExt;
import com.guldan.mapper.db.mapper.ProjectMapperExt;
import com.guldan.mapper.db.mapper.RegisterFailMapperExt;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Silas.
 * Date: 2016/12/3
 * Time: 17:36
 */
@Service
public class RegisterFailManager extends BaseManager {

    @Autowired
    private RegisterFailMapperExt registerFailMapperExt;

    @Autowired
    private AreaMapperExt areaMapperExt;

    @Autowired
    private ProjectMapperExt projectMapperExt;

    //列表查看所有登记失败人员的列表
    public Pagination<RegisterFailDTO> registFailList(RegisterFailSearchDTO searchDTO) {

        Pagination<RegisterFailDTO> result = new Pagination<RegisterFailDTO>(searchDTO.getCurrentPage(), searchDTO.getPerPage());
        RegisterFailExample example = buildSearchExample(searchDTO);
        int total = registerFailMapperExt.countByExample(example);
        result.setTotalCount(total);


        if (total > 0) {
            int projectId = 0;
            List<RegisterFailDTO> data = new ArrayList<RegisterFailDTO>();
            List<RegisterFail> list = registerFailMapperExt.selectByExampleWithPaging(example, result.getOffset(), result.getPageSize());
            int i = 0;
            for (RegisterFail entity : list) {
                RegisterFailDTO dto = RegisterFailDomainConvertUtils.convert(entity);

                if (i == 0) {
                    if (StringUtils.isNotBlank(dto.getProjectName())) {
                        ProjectExample projectExample = new ProjectExample();
                        ProjectExample.Criteria projectCriteria = projectExample.createCriteria();
                        projectCriteria.andProjectNameEqualTo(dto.getProjectName());
                        projectCriteria.andIsDeleteEqualTo(false);
                        List<Project> projectList = projectMapperExt.selectByExample(projectExample);
                        if (projectList != null && projectList.size() > 0) {
                            projectId = projectList.get(0).getProjectId();
                        }
                    }
                }
                i++;
                dto.setProjectId(projectId);
                data.add(dto);
            }
            result.setList(data);
        }
        return result;
    }


    //统计某个批次登记失败
    public Integer countRegisterFailByBatchId(Integer batchId) {
        Integer ret = registerFailMapperExt.countRegisterFailByBatchId(batchId);
        return ret;
    }

    //build查询Example
    private RegisterFailExample buildSearchExample(RegisterFailSearchDTO search) {
        RegisterFailExample example = new RegisterFailExample();
        RegisterFailExample.Criteria criteria = example.createCriteria();

        if (search.getBatchId() != null && search.getBatchId() > 0) {
            criteria.andBatchIdEqualTo(search.getBatchId());
        }
        if (search.getUploadId() != null && search.getUploadId() > 0) {
            criteria.andUploadIdEqualTo(search.getUploadId());
        }
        if (StringUtils.isNotBlank(search.getIdentNumber())) {
            criteria.andIdentNumberEqualTo(search.getIdentNumber());
        }
        if (StringUtils.isNotBlank(search.getName())) {
            criteria.andNameEqualTo(search.getName());
        }
        criteria.andIsDeleteEqualTo(Boolean.FALSE);
        if (StringUtils.isNotBlank(search.getOrderByClause())) {
            example.setOrderByClause(search.getOrderByClause());
        } else {
            example.setOrderByClause("register_id desc");
        }
        return example;
    }

    public RegisterFailDTO save(RegisterFailDTO dto) {
        RegisterFail entity;
        if (dto.getRegisterId() != null && dto.getRegisterId() > 0) {
            entity = registerFailMapperExt.selectByPrimaryKey(dto.getRegisterId());

            entity.setUploadId(dto.getUploadId());
            entity.setBatchId(dto.getBatchId());
            entity.setCitizenInfoId(dto.getCitizenInfoId());
            entity.setProjectName(dto.getProjectName());
            entity.setYear(dto.getYear());
            entity.setSubsidyCount(dto.getSubsidyCount());
            entity.setRegisterAreaName(dto.getRegisterAreaName());
            entity.setName(dto.getName());
            entity.setIdentNumber(dto.getIdentNumber());
            entity.setBankName(dto.getBankName());
            entity.setBankNumber(dto.getBankNumber());
            entity.setSubsidyMoney(dto.getSubsidyMoney());
            entity.setExtData(dto.getExtData());
            entity.setFailReason(dto.getFailReason());

            if (UserUtils.getUser() != null) {
                entity.setCreatorAreaId(UserUtils.getUser().getAreaId());
                entity.setModifier(UserUtils.getUser().getUserAccount());
            }
            entity.setDisabled(dto.getDisabled());
            entity.setModifyTime(DateUtil.getCurrentTime());

            registerFailMapperExt.updateByPrimaryKeySelective(entity);
        } else {
            entity = new RegisterFail();

            entity.setUploadId(dto.getUploadId());
            entity.setBatchId(dto.getBatchId());

            entity.setCitizenInfoId(dto.getCitizenInfoId());
            entity.setProjectName(dto.getProjectName());
            entity.setYear(dto.getYear());
            entity.setSubsidyCount(dto.getSubsidyCount());
            entity.setRegisterAreaName(dto.getRegisterAreaName());
            entity.setName(dto.getName());
            entity.setIdentNumber(dto.getIdentNumber());
            entity.setBankName(dto.getBankName());
            entity.setBankNumber(dto.getBankNumber());
            entity.setSubsidyMoney(dto.getSubsidyMoney());
            entity.setExtData(dto.getExtData());
            entity.setFailReason(dto.getFailReason());

            entity.setCreateTime(DateUtil.getCurrentTime());
            if (UserUtils.getUser() != null) {
                entity.setCreatorAreaId(UserUtils.getUser().getAreaId());
                entity.setCreator(UserUtils.getUser().getUserAccount());
            }

            registerFailMapperExt.insertSelective(entity);
        }
        dto = RegisterFailDomainConvertUtils.convert(entity);
        return dto;
    }
}
